package com.bhs.sansonglogistics.ui.quality;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.LinePromotionBean;
import com.bhs.sansonglogistics.bean.VoucherListBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.TopUpAdapter;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 推广记录
 */
public class LinePromotionListActivity extends BaseActivity implements NetCallBack {

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private int page = 1;
    private BaseQuickAdapter<LinePromotionBean.DataDTO.ListDTO, BaseViewHolder> mAdapter;

    @Override
    public int initLayout() {
        return R.layout.activity_line_promotion_list;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("充值记录");
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRvList = findViewById(R.id.rv_list);
    }

    Map<String, String> map = new ArrayMap<>();

    @Override
    public void initData() {
        mAdapter = new BaseQuickAdapter<LinePromotionBean.DataDTO.ListDTO, BaseViewHolder>(R.layout.item_line_promotion) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, LinePromotionBean.DataDTO.ListDTO item) {
                String startCity = item.getStart_city();
                String endCity = item.getEnd_city();
                if (item.getStart_city().length() > 4) {
                    startCity = startCity.substring(0, 4) + "...";
                }
                if (item.getEnd_city().length() > 4) {
                    endCity = endCity.substring(0, 4) + "...";
                }
                String info = String.format("%s—%s(%s)", startCity, endCity, MyUtils.getPromotionType(item.getPromote_type()));
                info = info.replace("市", "");
                helper.setText(R.id.tv_line_name, info);
                helper.setText(R.id.tv_money, String.format("￥%s", item.getMoney()));
                if (Double.parseDouble(item.getRedpack_money()) > 0) {
                    Spanned PageView = Html.fromHtml(MyUtils.getPayType(item.getPay_method()) + "<font color=\"#FF2330\">(红包抵扣￥-" + item.getRedpack_money() + ")</font>");
                    helper.setText(R.id.tv_type, PageView);
                } else {
                    helper.setText(R.id.tv_type, MyUtils.getPayType(item.getPay_method()));
                }
                helper.setText(R.id.tv_number, String.format("缴纳单号:%s", item.getOrder_sn()));
                helper.setText(R.id.tv_voucher_time, DateUtils.getTime2(item.getPay_time()));
                helper.setBackgroundRes(R.id.label, Integer.parseInt(item.getPromote_type()) > 5 ? R.drawable.bg_sliver_side : R.drawable.bg_gold_side);
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.layout_empty, null));
        loadData();
    }

    private void loadData() {
        map.put("page", String.valueOf(page));
        map.put("limit", "10");
        networkRequest(netApi.promote_list(map), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        LinePromotionBean bean = new Gson().fromJson(json, LinePromotionBean.class);
        mRefreshLayout.setRefreshing(false);
        if (bean.isStatus()) {
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}